package com.M10.BE.file;

import android.os.Environment;
import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/31.
 */

public class FileService {
    private static String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/asset/";
    static String fileName = "asset.txt";

    public static void write(String content) {
        if (!isExist()) {
            return;
        }
        File fs = new File(path + fileName);
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fs, true)));
            out.write(content);
            FileOutputStream outputStream = new FileOutputStream(fs);
            outputStream.write((content + "\n").getBytes());
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static List<String> readFile() {
        String strFilePath = path + fileName;
        List<String> newList = new ArrayList<String>();
        //打开文件
        File file = new File(strFilePath);
        //如果path是传递过来的参数，可以做一个非目录的判断
        if (!isExist()) {
            Log.d("FileService", "The File doesn't not exist.");
        } else {
            try {
                InputStream instream = new FileInputStream(file);
                if (instream != null) {
                    InputStreamReader inputreader = new InputStreamReader(instream);
                    BufferedReader buffreader = new BufferedReader(inputreader);
                    String line;
                    //分行读取
                    while ((line = buffreader.readLine()) != null) {
                        newList.add(line);
                    }
                    instream.close();
                }
            } catch (java.io.FileNotFoundException e) {
                Log.d("TestFile", "The File doesn't not exist.");
            } catch (IOException e) {
                Log.d("TestFile", e.getMessage());
            }
        }
        return newList;
    }

    public static void deleteFile() {
        File file = new File(path + fileName);
        if (file.exists()) {
            file.delete();
        }
    }


    private static boolean isExist() {
        boolean result = true;
        File file = new File(path);
        if (!file.exists()) {
            if (file.mkdirs()) {
                result = true;
            } else {
                return false;
            }
        }
        File fileAndName = new File(path + fileName);
        if (!fileAndName.exists()) {
            try {
                if (fileAndName.createNewFile()) {
                    result = true;
                } else {
                    result = false;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
